//
//  ProjectManager.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/5/3.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#define ProjectFilepath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"project.data"]

#import "ProjectManager.h"

@implementation ProjectManager {
    int currentStep;
    int totalSteps;
    int template_bind;
    BOOL is_new_project;
}

+(ProjectManager *) manager{
    static ProjectManager *project_manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        project_manager = [[ProjectManager alloc] init];
    });
    return project_manager;
}

/**
 *  归档项目信息
 *
 */
- (void)saveProject:(ProjectData *)project{
    //归档
    [NSKeyedArchiver archiveRootObject:project toFile:ProjectFilepath];
    CMLog(@"归档成功");
}

/**
 *  获取项目信息
 *
 *  @return 项目信息
 */
- (ProjectData *)project{
    //反归档
    ProjectData *projectInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:ProjectFilepath];
    return projectInfo;
}

- (NSString*)projectId {
    ProjectData *data = [self project];
    return data.project_id;
}

- (ProjectData*)newProject {
    ProjectData *data = [ProjectData new];
    data.requirement = @"空调";
    [self saveProject:data];
    return data;
}


- (void)loadProjectInfoByGuid:(NSString*)guid result:(void (^)(id))result {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:guid forKey:@"pid"];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_BY_GUID token:[[AccountManager manager]token] paramDic:param resultClass:[ProjectData class] success:^(ProjectData *obj) {
        [self saveProject:obj.getProjectResult];
        return result(obj.getProjectResult);
    } failure:^(NSError *error) {
        CMLog(@"获取数据错误：%@", error);
        return result(nil);
    }];
}


- (void)loadProjectInfoByGuid:(NSString*)guid andStatus:(KKPROJECT_STATUS)status{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:guid forKey:@"pid"];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_BY_GUID token:[[AccountManager manager]token] paramDic:param resultClass:[ProjectData class] success:^(ProjectData *obj) {
        obj.getProjectResult.status = status;
        [self saveProject:obj.getProjectResult];
    } failure:^(NSError *error) {
        CMLog(@"获取数据错误：%@", error);
    }];
}

- (void)modifyProjectInfoSuccess:(void (^)(void))success failure:(void (^)(NSError *))failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *project = [[self project] keyValues];
    [project setValue:[NSNumber numberWithInt:1] forKey:@"phase"];
    
    [params setObject:project forKey:@"prjInfo"];
    
    [Tools printJsonStr:params];
    
    [HttpClient postWithUrl:HTTP_POST_PROJECT_INFO token:[[AccountManager manager] token] paramDic:params resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
        [[ProjectManager manager]loadProjectInfoByGuid:obj.createResult andStatus:KPROJECT_STATUS_NEW];
        success();
    } failure:^(NSError *error) {
        [Tools showMessageCenter:@"修改项目失败，请重试！"];
        failure(error);
        CMLog(@"错误信息为：%@",error);
    }];

}

- (KKPROJECT_STATUS)getProjectStatus {
    ProjectData *data = [self project];
    return data.status;
}

- (void)setProjectStatus:(KKPROJECT_STATUS)status {
    ProjectData *data = [self project];
    data.status = status;
    if (status == KPROJECT_STATUS_MODIFY) {
        data.isBindTemplate = YES;
    }
    [self saveProject:data];
}

- (void)bindProjectTemplate {
    ProjectData *data = [self project];
    data.isBindTemplate = YES;
    [self saveProject:data];
}

- (BOOL)isBindProjectTemplate {
    ProjectData *data = [self project];
    return data.isBindTemplate;
}


/**
 *  是否显示室内机设备
 *
 *  @return 依赖于室外机开关是否enable
 */
- (BOOL)displaySNJMachine {
    ProjectData *data = [self project];
    return data.swjEnable;
}

- (BOOL)displayQXSYKTMachine {
    ProjectData *data = [self project];
    return data.qxsyktEnable;
}

/**
 *  清除账号信息
 */
- (void)clearProjectInfo{
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    if ([defaultManager isDeletableFileAtPath:ProjectFilepath]) {
        [defaultManager removeItemAtPath:ProjectFilepath error:nil];
    }
}


/**
 *
 
 系统配置中：
 
 1.室外机开关关闭
 房间中，室内设备不允许添加室内机，室外机系统配置不允许
 
 2.轻型商用空调开关关闭
 房间中，室内设备不允许添加轻型商用空调。
 
 3.如果室外机开关和轻型商用空调开关均关闭
 不显示房间信息页面和室外机系统配置页面。
 
 4.空气能热水机开关关闭
 不显示空气能配置页面
 
 5.开关关闭
 不显示页面配置
 
 */
- (BOOL)snjDeviceEnabled {
    ProjectData *data = [self project];
    return data.swjEnable;
}
- (BOOL)qxsyktDeviceEnabled {
    ProjectData *data = [self project];
    return data.qxsyktEnable;
}
- (BOOL)swjPageEnabled {
    ProjectData *data = [self project];
    return data.swjEnable;
}
- (BOOL)roomPageEnabled {
    ProjectData *data = [self project];
    BOOL isEnable = YES;
    if (!data.swjEnable && !data.qxsyktEnable) {
        isEnable = NO;
    }
    return isEnable;
}
- (BOOL)kqnrsjPageEnabled {
    ProjectData *data = [self project];
    return data.kqnrsjEnable;
}
- (BOOL)qrjhqPageEnabled {
    ProjectData *data = [self project];
    return data.qrjhqEnable;
}

- (void)initStepInfo:(BOOL)is_new{
    template_bind = 0;
    currentStep = 1;
    totalSteps = 8;
    is_new_project = is_new;
}

- (void)setTemplateBind:(int)bind {
    template_bind = bind;
    if (is_new_project) {
        if (template_bind == 1 && totalSteps == 8) {
            totalSteps = 7;
        }
        return;
    }
    [self updateTotalSteps];
}

- (void)updateTotalSteps {
    if (template_bind == 1) {
        totalSteps = 7;
    } else {
        totalSteps = 8;
    }
    if ([Tools isEmpty:kProject.main_equip_series]) {
        totalSteps--;
    }
    if ([Tools isEmpty:kProject.qrjhq_series]) {
        totalSteps--;
    }
    if ([Tools isEmpty:kProject.kqnrsj_series]) {
        totalSteps--;
    }
    if ([Tools isEmpty:kProject.water_cleaner_series]) {
        totalSteps--;
    }
}

- (void)goPage:(int)step {
    currentStep = step;
}

- (void)goLastPage {
    if (currentStep > 1) {
        currentStep--;
    }
}

- (void)goNextPage {
    if (currentStep < totalSteps) {
        currentStep++;
    }
}


- (NSString*)getCurrentStepInfo {
    NSString *ret = [NSString stringWithFormat:@"%d/%d", currentStep, totalSteps];
    return ret;
}

@end
